<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('设置列表')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="user-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">键</label>
                <div class="layui-input-inline">
                    <input type="text" name="setKey" placeholder="键" class="layui-input">
                </div>
                <label class="layui-form-label">值</label>
                <div class="layui-input-inline">
                    <input type="text" name="setValue" placeholder="值" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary 字典名称" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md 字典名称">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="user-main ">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="user-table" lay-filter="user-table"></table>
        </div>
    </div>
</div>
</body>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="pear-icon pear-icon-add"></i>
        新增
    </button>
</script>

<script type="text/html" id="user-bar">
    <div class="pear-btn-group">
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="pear-icon pear-icon-edit"></i>
        </button>
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="pear-icon pear-icon-ashbin"></i>
        </button>
    </div>

</script>


<th:block th:include="include :: footer"/>
<script th:inline="javascript">

    var datas = [[${@dict.getDict('sex')}]];

    layui.use(['table', 'dtree', 'form', 'jquery', 'popup', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let common = layui.common;


        let cols = [
            [
                {title: '键', field: 'setKey', align: 'center'},
                {title: '值', field: 'setValue', align: 'center'},
                {title: '备注', field: 'remark', align: 'center'},
                {title: '创建时间', field: 'createTime', align: 'center'},
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 130}
            ]
        ]

        // 字典
        window.dictTrans = function (val) {
            for (let i = 0; i < datas.length; i++) {
                if (val == datas[i].dataValue) {
                    return datas[i].dataLabel;
                }
            }
        }


        table.render({
            elem: '#user-table',
            url: suffix + '/system/set/list',
            page: true,
            cols: cols,
            skin: 'line',
            height: 'full-148',
            toolbar: '#user-toolbar',
            text: {none: '暂无信息'},
            defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}, 'filter', 'print', 'exports']
        });

        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event == 'collasped') {
                $(".user-left").toggleClass("user-collasped");
                $(".user-main").toggleClass("user-collasped");
                table.resize();
            }
        });

        form.on('submit(user-query)', function (data) {
            window.refresh(data.field);
            return false;
        });

        form.on('switch(user-enable)', function (obj) {
            let operate;
            if (obj.elem.checked) {
                operate = "enable";
            } else {
                operate = "disable";
            }
            let loading = layer.load();
            $.ajax({
                url: suffix + '/system/system/user/' + operate,
                data: JSON.stringify({userId: this.value}),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    layer.close(loading);
                    if (result.success) {
                        popup.success(result.msg);
                    } else {
                        popup.failure(result.msg);
                    }
                }
            })
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                shadeClose: true,
                area: ['550px', '550px'],
                content: suffix + '/system/set/add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                shadeClose: true,
                area: ['550px', '500px'],
                content: suffix + '/system/set/edit?setKey=' + obj.data['setKey']
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除', {
                icon: 3,
                shadeClose: true,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: suffix + "/system/set/delete",
                    dataType: 'json',
                    data: {key: obj.data['setKey']},
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function () {
                                obj.del();
                            });
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let ids = common.checkField(obj, 'userId');
            if (common.isEmpty(ids)) {
                popup.warning("未选中数据");
                return false;
            }
            layer.confirm('确定要删除选中用户', {icon: 3, shadeClose: true, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: suffix + "/system/batchRemove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            popup.success(result.msg, function () {
                                table.reload('user-table');
                            })
                        } else {
                            popup.failure(result.msg);
                        }
                    }
                })
            });
        }

        // 重置
        $("button[type='reset']").click(() => {
            let data = form.val("user-query-form")
            Object.keys(data).forEach(key => (data[key] = ''));
            window.refresh(data);
        })

        window.refresh = function (param) {
            table.reload('user-table', {where: param});
        }
    })
</script>
</html>
